package com.maszy.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConnectionManager {
	protected static Log logger = LogFactory.getLog(ConnectionManager.class);

	private static BasicDataSource dataSource = null;

	private ConnectionManager() {

	}
	/*
	dataSource.removeAbandoned = true;
    dataSource.removeAbandonedTimeout = 300;
	dataSource.testOnBorrow = true ;
	dataSource.testOnReturn = true ;
	dataSource.testWhileIdle = true;
	dataSource.validationQuery = "select 1";
	dataSource.logAbandoned = true;
	dataSource.timeBetweenEvictionRunsMillis = 5000;
	dataSource.minEvictableIdleTimeMillis = 4000;
	 */
	private static void setupDataSource() {
		if (dataSource != null)
			return;

		dataSource = new BasicDataSource();

		String initialSize = Constants.DATASOURCE_INITIALSIZE;
		String minIdle = Constants.DATASOURCE_MINIDLE;
		String maxIdle = Constants.DATASOURCE_MAXIDLE;
		String maxWait = Constants.DATASOURCE_MAXWAIT;
		String maxActive = Constants.DATASOURCE_MAXACTIVE;
		
		String removeAbandoned = Constants.DATASOURCE_REMOVEABANDONED;
		String removeAbandonedTimeout = Constants.DATASOURCE_REMOVEABANDONEDTIMEOUT;
		String testOnBorrow = Constants.DATASOURCE_TESTONBORROW;
		String testOnReturn = Constants.DATASOURCE_TESTONBORROW;
		String testWhileIdle  =Constants.DATASOURCE_TESTWHILEIDLE;
		String validationQuery = Constants.DATASOURCE_VALIDATIONQUERY;
		String logAbandoned = Constants.DATASOURCE_LOGABANDONED;
		String timeBetweenEvictionRunsMillis = Constants.DATASOURCE_TIMEBETWEENEVICTIONRUNSMILLIS;
		String minEvictableIdleTimeMillis = Constants.DATASOURCE_MINEVICTABLEIDLETIMEMILLIS;
		

		dataSource.setDriverClassName(Constants.JDBC_DRIVER);
		dataSource.setUrl(Constants.JDBC_URL);
		dataSource.setUsername(Constants.JDBC_USERNAME);
		dataSource.setPassword(Constants.JDBC_PASSWORD);

		if (initialSize != null)
			dataSource.setInitialSize(Integer.parseInt(initialSize));

		if (minIdle != null)
			dataSource.setMinIdle(Integer.parseInt(minIdle));

		if (maxIdle != null)
			dataSource.setMaxIdle(Integer.parseInt(maxIdle));

		if (maxWait != null)
			dataSource.setMaxWait(Long.parseLong(maxWait));

		if (maxActive != null) {
			if (!maxActive.trim().equals("0"))
				dataSource.setMaxActive(Integer.parseInt(maxActive));
		}
		if (removeAbandoned != null)
			dataSource.setRemoveAbandoned(Boolean.parseBoolean(removeAbandoned));
		
		if(removeAbandonedTimeout != null)
			dataSource.setRemoveAbandonedTimeout(Integer.parseInt(removeAbandonedTimeout));
		
		if(testOnBorrow != null) 
			dataSource.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow));
		
		if(testOnReturn != null)
			dataSource.setTestOnReturn(Boolean.parseBoolean(testOnReturn));
		
		if(testWhileIdle != null)
			dataSource.setTestWhileIdle(Boolean.parseBoolean(testWhileIdle));
		
		if(validationQuery != null)
			dataSource.setValidationQuery(validationQuery);
		
		if(logAbandoned != null)
			dataSource.setLogAbandoned(Boolean.parseBoolean(logAbandoned));
		
		if(timeBetweenEvictionRunsMillis != null)
			dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));
		
		if(minEvictableIdleTimeMillis != null)
			dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(minEvictableIdleTimeMillis));

	}
	
	public static DataSource getDataSource() {
		if (dataSource == null)
			setupDataSource();
		return dataSource;
	}
	
	public static Connection getConnection() {
		if (getDataSource() != null) {
			try {
				return getDataSource().getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
				logger.error("t����ݿ�ʧ��:"+ e.getMessage(), e);
			}
		}
		return null;
	}

	public static void closeConnection(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
				conn = null;
				e.printStackTrace();
				//logger.error("�ر���ݿ�����쳣��" + e.getMessage(), e);
			}
		}else{
			
		}
	}
}
